package pl.bizuteria.sklep.admin.client;

import pl.bizuteria.sklep.admin.client.activity.mapper.CreateOrEditActivityMapper;
import pl.bizuteria.sklep.admin.client.activity.mapper.ProductActivityMapper;
import pl.bizuteria.sklep.admin.client.place.ProxyCreatePlace;
import pl.bizuteria.sklep.admin.client.place.ProxyEditPlace;
import pl.bizuteria.sklep.admin.client.place.ProxyListPlace;
import pl.bizuteria.sklep.admin.client.ui.ITwoSlotView;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.activity.shared.CachingActivityMapper;
import com.google.gwt.activity.shared.FilteredActivityMapper;
import com.google.gwt.activity.shared.FilteredActivityMapper.Filter;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;

public class ProductModule implements Module {

	ITwoSlotView twoSlotView;

	private EventBus eventBus;

	@Inject
	public ProductModule(ITwoSlotView twoSlotView, EventBus eventBus,
			CreateOrEditActivityMapper createOrEditActivityMapper,
			ProductActivityMapper productActivityMapper) {
		super();
		this.twoSlotView = twoSlotView;
		this.eventBus = eventBus;
		this.createOrEditActivityMapper = createOrEditActivityMapper;
		this.productActivityMapper = productActivityMapper;
		init();
	}

	CreateOrEditActivityMapper createOrEditActivityMapper;
	ProductActivityMapper productActivityMapper;

	public IsWidget getView() {
		return twoSlotView;
	}

	private ActivityMapper createFilteredProductActivityMapper() {
		CachingActivityMapper cachingActivityMapper = new CachingActivityMapper(
				productActivityMapper);
		Filter filter = new Filter() {

			public Place filter(Place place) {
				if (place instanceof ProxyListPlace) {
					return (ProxyListPlace) place;
				} else if (place instanceof ProxyEditPlace) {
					ProxyEditPlace proxyEditPlace = (ProxyEditPlace) place;
					return new ProxyListPlace(proxyEditPlace.getStableId()
							.getProxyClass());
				} else if (place instanceof ProxyCreatePlace) {
					ProxyCreatePlace createPlace = (ProxyCreatePlace) place;
					return new ProxyListPlace(createPlace.getProxyType());
				}
				return null;
			}
		};
//		 FilteredActivityMapper filteredActivityMapper = new
//		 FilteredActivityMapper(filter, cachingActivityMapper);
		FilteredActivityMapper filteredActivityMapper = new FilteredActivityMapper(
				filter, productActivityMapper);
		return filteredActivityMapper;
	}

	public void init() {

		ActivityMapper filteretProductActivityMapper = createFilteredProductActivityMapper();

		ActivityManager activityManagerx1 = new ActivityManager(
				filteretProductActivityMapper, eventBus);
		activityManagerx1.setDisplay(twoSlotView.getTopPanel());

		// details
		ActivityManager activityManagerx2 = new ActivityManager(
				createOrEditActivityMapper, eventBus);
		activityManagerx2.setDisplay(twoSlotView.getBottomPanel());
	}

	public boolean handlesPlace(Place place) {
		return (place instanceof ProxyCreatePlace  || place instanceof ProxyEditPlace || place instanceof ProxyListPlace);
	}

}
